package jp.co.dailyReport.dailyReport;

import java.sql.SQLException;
import org.apache.log4j.Logger;
import jp.co.dailyReport.ExecuterManager;
import jp.co.dailyReport.ConnectionFactory;
import jp.co.dailyReport.DataUnavailableException;
import jp.co.dailyReport.dailyReport.sql.DailyReportRestTimeLoadExecuter;
import jp.co.dailyReport.dailyReport.sql.DailyReportWorkItemInsertExecuter;
import jp.co.dailyReport.dailyReport.sql.DailyReportWorkItemsLoadExecuter;
import jp.co.dailyReport.dailyReport.sql.DailyReportWorkItemDeleteExecuter;

/**
 * 日報作業項目情報管理クラス
 */
public class DailyReportWorkItemManager {

	private static Logger log = Logger.getLogger(DailyReportManager.class);

	/**
	 * 日報作業項目情報を取得します。
	 * 
	 * @param dailyReportId 日報ID
	 * @return 日報作業項目オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static double loadRestTime(int dailyReportId) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		DailyReportRestTimeLoadExecuter dailyReportRestTimeLoadExecuter = new DailyReportRestTimeLoadExecuter(dailyReportId);
		Object objs[] = (Object[]) null;
		try {
			objs = ExecuterManager.execute(connectionFactory, dailyReportRestTimeLoadExecuter);
		} catch (SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		double restTime = 0.0d;
		if (objs == null || objs.length == 0) {
			return restTime;
		} else {
			for(int i=0; i<objs.length; i++) {
				restTime += ((Double)objs[i]).doubleValue();
			}
			return restTime;
		}
	}

	/**
	 * 日報作業項目情報を取得します。
	 * 
	 * @param dailyReportId 日報ID
	 * @return 日報作業項目オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static DailyReportWorkItem[] load(int dailyReportId) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		DailyReportWorkItemsLoadExecuter dailyReportWorkItemsLoadExecuter = new DailyReportWorkItemsLoadExecuter(dailyReportId);
		Object objs[] = (Object[]) null;
		try {
			objs = ExecuterManager.execute(connectionFactory, dailyReportWorkItemsLoadExecuter);
		} catch (SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if (objs == null || objs.length == 0) {
			return new DailyReportWorkItem[0];
		} else {
			return (DailyReportWorkItem[]) objs;
		}
	}

	/**
	 * 日報作業項目を登録します。
	 * 
	 * @param dailyReportWorkItem 日報作業項目オブジェクト
	 * @throws DataUnavailableException
	 */
	public static void insert(DailyReportWorkItem dailyReportWorkItem) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		DailyReportWorkItemInsertExecuter dailyReportWorkItemInsertExecuter = new DailyReportWorkItemInsertExecuter(dailyReportWorkItem);

		try {
			ExecuterManager.execute(connectionFactory, dailyReportWorkItemInsertExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
	}

	/**
	 * 日報作業項目を削除します。
	 * 
	 * @param dailyReportId 日報ID
	 * @throws DataUnavailableException
	 */
	public static void delete(int dailyReportId) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		DailyReportWorkItemDeleteExecuter dailyReportWorkItemDeleteExecuter = new DailyReportWorkItemDeleteExecuter(dailyReportId);

		try {
			ExecuterManager.execute(connectionFactory, dailyReportWorkItemDeleteExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
	}

	/**
	 * 作業時間の合計値を求めます。
	 * 
	 * @param dailyReportWorkItem 日報作業項目オブジェクトの配列
	 * @return 作業時間の合計値
	 */
	public static double calculate(DailyReportWorkItem[] dailyReportWorkItem) {

		double totalWorkingHours = 0.0D;
		for(int i = 0; i < dailyReportWorkItem.length; i++) {
			totalWorkingHours += dailyReportWorkItem[i].getWorkingHours();
		}
		return totalWorkingHours;
	}
}
